import {
    PRODUCT_LIST,
    PRODUCT_LIST_OUTSALE,
    PRODUCT_LIST_INCHECK,
    PRODUCT_CATEGORY,
    PRODUCT_CATEGORY_CHOOSE,
    PRODUCT_BRAND_CHOOSE,
    PRODUCT_LIST_INEDIT,
    PRODUCT_BRAND,
    Property_Set,
    Property_Refresh,
    Product_Proprty,
    Property_Save
}
    from
        './constants';
import NetUtil from '../../common/NetUtil';

import config from '../../config/config';

import {getGoodsCategory} from '../../utils/NativeUtil';

export const getProductList = (params, productstate, callback) => {
    return dispatch => {
        let url = '/s/goods/list';
        const pageSize = 5;
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                if (res.data.length < pageSize) {
                    callback({hasMore: false, footText: "加载完成"})
                } else {
                    callback({hasMore: true, footText: "加载更多"})
                }
                if (productstate == '1') {
                    dispatch({
                        type: PRODUCT_LIST,
                        payload: {
                            productOnSale: {
                                page: params.page,
                                list: res.data
                            }
                        }
                    })
                } else if (productstate == '0') {
                    dispatch({
                        type: PRODUCT_LIST_OUTSALE,
                        payload: {
                            productOutSale: {
                                page: params.page,
                                list: res.data
                            }
                        }
                    })
                } else if (productstate == '3') {
                    dispatch({
                        type: PRODUCT_LIST_INEDIT,
                        payload: {
                            productInEdit: {
                                page: params.page,
                                list: res.data
                            }
                        }
                    })
                }else {
                    dispatch({
                        type: PRODUCT_LIST_INCHECK,
                        payload: {
                            productInCheck: {
                                page: params.page,
                                list: res.data
                            }
                        }
                    })
                }
                // callback(false,"加载完成");
            } else {
                callback({hasMore: false, footText: "加载失败，下拉重新加载"})
            }
            // if (isrefresh) { self.endLoading(); }
        });
    }
};

export const getProductDetail = (params, callback) => {
    let url = '/s/goods/detail';
    NetUtil.postSign(config.apiUrl + url, params, function (res) {
        if (res.success) {
            callback(res.data);
        }
    });
}

export const addProduct = (params, callback) => {
    return dispatch => {
        let url = '/s/goods/add';

        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                callback(res);
            }
        });
    }
};
/**
 * 更新商品
 * @param params
 * @param callback
 * @returns {function(*)}
 */
export const updateProduct = (params, callback) => {
    return dispatch => {
        let url = '/s/goods/base_edit';
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                callback();
            }
        });
    }
};

export const delProduct = (params, callback) => {
    let url = '/s/goods/delete';

    NetUtil.postSign(config.apiUrl + url, params, function (res) {
        if (res.success) {
            callback();
        }
    });
};

export const updateSoldOut = (params, callback) => {
    let url = '/s/goods/edit_on_sale';
    NetUtil.postSign(config.apiUrl + url, params, function (res) {
        if (res.success) {
            callback();
        } else {
        }
    });
};

export const getProductCategorys = (pid, callback) => {
    return dispatch => {
        let params = {
            parentId: pid
        };
        getGoodsCategory(params, (data) => {
            if (data.list.length == 0) {
                if (callback != undefined) {
                    callback();
                }
            } else {
                dispatch({
                    type: PRODUCT_CATEGORY,
                    payload: {
                        productCategorys: data.list
                    }
                })
            }
        })
    }
};

export const getProductCategoryDetail = (catCode) => {
    return dispatch => {

        let params = {
            code: catCode
        };

        getGoodsCategory(params, (data) => {
            dispatch(chooseCategory(data));
        })
    }
};

export const chooseCategory = (item) => {
    return dispatch => {
        dispatch({
            type: PRODUCT_CATEGORY_CHOOSE,
            payload: {
                chooseCategory: item
            }
        })
    }
};

export const getProductBrands = () => {
    return dispatch => {
        let url = '/s/brand/getAll';
        const params = {};
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                dispatch({
                    type: PRODUCT_BRAND,
                    payload: {
                        productBrands: res.data
                    }
                })
            } else {}
        });
    }
}

export const chooseBrand = (item) => {
    return dispatch => {
        dispatch({
            type: PRODUCT_BRAND_CHOOSE,
            payload: {
                chooseBrand: item
            }
        })
    }
};

export const initProperty = (list) => {
    return dispatch => {
        dispatch({
            type: Product_Proprty,
            payload: {
                propertyList: list
            }
        })
    }
};

export const setProperty = (item) => {
    return dispatch => {
        dispatch({
            type: Property_Set,
            payload: {
                propertyItem: item
            }
        })
    }
};

//刷新数据
export const refreshProperty = () => {
    return dispatch => {
        dispatch({
            type: Property_Refresh
        })
    }
};

//保存商品属性
export const saveGoodsAttr = (params, callback) => {
    return dispatch => {
        let url = '/s/goods_attr/mul_save';
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            console.log(res);
            if (res.success) {
                dispatch({
                    type: Property_Save,
                    payload: {
                        Products: res.data.products
                    }
                });
                callback();
            }
        });
    }
};

//产品批量保存
export const saveProducts = (params, callback) => {
    return dispatch => {
        let url = "/s/product/mul_save";
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            callback();
        })
    }
};


// 编辑商品详情
export const saveProductDetail = (params, callback) => {
    return dispatch => {
        let url = "/s/goods/edit_desc";
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            callback();
        });
    };
};

/**
 * 提交审核
 */
export const submitProduct = (params, callback) => {
    return dispatch => {
        let url = "/s/goods/submit";
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            callback(res);
        })
    }
};


/**
 * 获取商品属性
 **/
export const getProductProperty = (params, callback) => {
    return dispatch => {
        let url = "/s/goods_attr/list_by_goods";
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            callback(res);
        })
    }
};